import 'dart:async';
import 'package:flutter_component_pay/flutter_component_pay.dart';
import 'package:flutter_component_pay/src/utils/abs_alipay_utils.dart';
import 'package:flutter_component_pay/src/utils/abs_wxPay_utils.dart';

/* 
* 用于打开第三支付平台工具
*
* 使用微信支付前时，必须初始化 AbsWXPay.initWXPay(appId, universalLink);
*
    # 配置在主项目中 pubspec.yaml。iOS还需要配置白名单
    # fluwx:
    #   app_id: iOS关于app bundleid对应在微信支付注册后，生成的appid
    #   debug_logging: false # Logging in debug mode.
    #   android:
    #     # interrupt_wx_request: true # Defaults to true.
    #     # flutter_activity: 'MainActivity' # Defaults to app's launcher
    #   ios:
    #     universal_link: 配置指定域名路径直接唤醒APP，生成方式可查相关文档。xcode中配置Associaed Domains
    # tobias:
    #   url_scheme: iOS中info.plist底部配置的URL Types，对于alipay的URL Schemes
    #   ios:
    #     ignore_security: true
*/
class AbsPayUtils {
  /// 初始化微信支付回调流
  static Stream<bool> get wxInitPayStream => AbsWxPay.wxInitEventHandler;
  // 未安装微信回调流
  static Stream<bool> get wxNotInstallStream => AbsWxPay.wxNotInstallEventHandler;
  // 打开微信支付回调流
  static Stream<bool> get wxOpenStream => AbsWxPay.wxOpenEventHandler;
  // 微信支付后，返回数据流
  static Stream<AbsPayResult> get wxResponseStream => AbsWxPay.wxResponseEventHandler;

  /// 未安装支付宝回调流
  static Stream<bool> get aliNotInstallStream => AbsAlipay.aliNotInstallEventHandler;
  // 打开支付宝支付回调流
  static Stream<bool> get aliOpenStream => AbsAlipay.aliOpenEventHandler;
  // 支付宝支付后，返回数据流
  static Stream<AbsPayResult> get aliResponseStream => AbsAlipay.aliResponseEventHandler;

  /// 初始化微信支付
  static void initWXPay({
    required String appId,
    required String universalLink,
  }) =>
      AbsWxPay.initWXPay(appId: appId, universalLink: universalLink);

  ///
  /// 开启三方支付平台
  ///
  /// * [way] 支付平台
  ///
  /// * [paramsChannel] 支付参数模型
  ///
  static void openPayPlatform(
    AbsPayWay way, {
    required AbsPayParamsChannel paramsChannel,
  }) {
    switch (way) {
      case AbsPayWay.wxPay: // 微信支付
        AbsWxPay.openWxPay(paramsChannel: paramsChannel);
        break;
      case AbsPayWay.aliPay: // 支付宝支付
        AbsAlipay.openAlipay(paramsChannel);
        break;
      case AbsPayWay.union: // 银联支付
        break;

      default:
        break;
    }
  }

  /// 关闭微信/支付宝订阅流
  /// 界面销毁后，同时关闭StreamController【.close()】
  static void closeStreamControllers() {
    AbsWxPay.closeWxStreamControllers();
    AbsAlipay.closeWxStreamControllers();
  }
}
